// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// FREE SOURCE CODE
// http://www.magic-software.com/License/free.pdf

#ifndef MGCCURVE_H
#define MGCCURVE_H

#include "MgcMath.h"


template <class Vector>
class MgcCurve
{
public:
    // abstract base class
    MgcCurve (MgcReal fTMin, MgcReal fTMax);
    virtual ~MgcCurve ();

    // interval on which curve parameter is defined
    MgcReal GetMinTime () const;
    MgcReal GetMaxTime () const;

    // position and derivatives
    virtual Vector GetPosition (MgcReal fTime) const = 0;
    virtual Vector GetFirstDerivative (MgcReal fTime) const = 0;
    virtual Vector GetSecondDerivative (MgcReal fTime) const = 0;
    virtual Vector GetThirdDerivative (MgcReal fTime) const = 0;

    // differential geometric quantities
    Vector GetTangent (MgcReal fTime) const;
    MgcReal GetSpeed (MgcReal fTime) const;
    virtual MgcReal GetLength (MgcReal fT0, MgcReal fT1) const = 0;
    MgcReal GetTotalLength () const;

    // inverse mapping of s = Length(t) given by t = Length^{-1}(s)
    virtual MgcReal GetTime (MgcReal fLength, int iIterations = 32,
        MgcReal fTolerance = 1e-06) const = 0;

    // subdivision
    void SubdivideByTime (int iNumPoints, Vector*& rakPoint) const;
    void SubdivideByLength (int iNumPoints, Vector*& rakPoint) const;

    // Subdivision by variation. The pointers pkP0 and pkP1 correspond to the
    // curve points at fT0 and fT1.  If the pointer values are not null, the
    // assumption is that the caller has passed in the curve points.
    // Otherwise, the function computes the curve points.
    virtual MgcReal GetVariation (MgcReal fT0, MgcReal fT1,
        const Vector* pkP0 = 0, const Vector* pkP1 = 0) const = 0;
    void SubdivideByVariation (MgcReal fMinVariation,
        unsigned int uiMaxLevel, int& iNumPoints, Vector*& rakPoint) const;

protected:
    // curve parameter is t where tmin <= t <= tmax
    MgcReal m_fTMin, m_fTMax;

    // subdivision
    class PointList
    {
    public:
        PointList (const Vector& rkPoint)
        {
            m_kPoint = rkPoint;
            m_kNext = 0;
        }

        Vector m_kPoint;
        PointList* m_kNext;
    };

    void SubdivideByVariation (MgcReal fT0, const Vector& rkP0,
        MgcReal fT1, const Vector& rkP1, MgcReal kMinVariation,
        unsigned int uiLevel, int& iNumPoints, PointList*& rpkList) const;
};

#include "MgcCurve.inl"

#endif
